package com.haixiaoke.saas.house.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.cleaner.domain.Cleaner;
import com.haixiaoke.saas.cleaner.domain.InspectionProcessRecord;
import com.haixiaoke.saas.cleaner.domain.SweepProcessRecord;
import com.haixiaoke.saas.cleaner.service.ICleanerService;
import com.haixiaoke.saas.cleaner.service.ISweepProcessRecordService;
import com.haixiaoke.saas.common.config.sms.SmsConfig;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.constant.TenantConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.CleanerEnum;
import com.haixiaoke.saas.common.enums.HouseInspectionEnum;
import com.haixiaoke.saas.common.enums.HouseSweepEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.sms.SmsUtils;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.house.domain.*;
import com.haixiaoke.saas.house.mapper.HouseSweepMapper;
import com.haixiaoke.saas.house.service.ICleaningProcessService;
import com.haixiaoke.saas.house.service.IHousePictureService;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.house.service.IHouseSweepService;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.service.IOrderService;
import com.haixiaoke.saas.user.domain.BizUserBusiness;
import com.haixiaoke.saas.user.service.IBizUserBusinessService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 打扫记录Service业务层处理
 *
 * @author xiaolong
 * @date 2024-05-11
 */
@Service
public class HouseSweepServiceImpl implements IHouseSweepService {
    @Autowired
    private HouseSweepMapper houseSweepMapper;
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICleaningProcessService cleaningProcessService;
    @Autowired
    private ISweepProcessRecordService sweepProcessRecordService;
    @Autowired
    private ICleanerService cleanerService;
    @Autowired
    private IBizUserBusinessService bizUserBusinessService;
    @Autowired
    private SmsUtils smsUtils;
    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private ITenantService departmentService;
    @Autowired
    private IHousePictureService housePictureService;

    /**
     * 查询打扫记录
     *
     * @param houseSweepId 打扫记录主键
     * @return 打扫记录
     */
    @Override
    public HouseSweep selectHouseSweepByHouseSweepId(String houseSweepId) {
        HouseSweep houseSweep = houseSweepMapper.selectHouseSweepByHouseSweepId(houseSweepId);
        if (ObjectUtil.isEmpty(houseSweep)) {
            throw new ServiceException("数据已被删除，请刷新后再试");
        }
        SweepProcessRecord sweepProcessRecord = new SweepProcessRecord();
        sweepProcessRecord.setHouseSweepId(houseSweepId);
        sweepProcessRecord.setTenantId(TenantContext.getTenantId());
        List<SweepProcessRecord> sweepProcessRecordList = sweepProcessRecordService.selectSweepProcessRecordList(sweepProcessRecord);
        houseSweep.setSweepProcessRecordList(sweepProcessRecordList);
        return houseSweep;
    }

    /**
     * 查询打扫记录列表
     *
     * @param houseSweep 打扫记录
     * @return 打扫记录
     */
    @Override
    public List<HouseSweep> selectHouseSweepList(HouseSweep houseSweep) {
        return houseSweepMapper.selectHouseSweepList(houseSweep);
    }

    /**
     * 新增打扫记录
     *
     * @param houseSweep 打扫记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHouseSweepInfo(HouseSweep houseSweep) {
        if (houseSweep.getScheduledTime() == null || houseSweep.getScheduledTime().equals("")) {
            throw new ServiceException("请选择预计开始时间");
        }
        Order order = orderService.selectOrderById(houseSweep.getOrderId());
        if (order == null) {
            throw new ServiceException("订单不存在");
        }
        House house = houseService.selectHouseById(order.getHouseId());
        if (house == null) {
            throw new ServiceException("房源不存在");
        }
        Tenant tenant = departmentService.selectDepByDepId(TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            throw new ServiceException("租户不存在");
        }

        CleaningProcess cleaningProcess = new CleaningProcess();
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        cleaningProcess.setHouseId(house.getHouseId());
        List<CleaningProcess> cleaningProcessList = cleaningProcessService.selectCleaningProcessListAndParentId(cleaningProcess);
        if (CollectionUtil.isEmpty(cleaningProcessList)) {
            throw new ServiceException("房间未设置保洁流程");
        }


        // 自动派单
        if (TenantConstants.ORDER_ALLOCATION_STATE_YES.equals(tenant.getOrderAllocationState())) {
            Cleaner cleaner = new Cleaner();
            cleaner.setTenantId(TenantContext.getTenantId());
            cleaner.setCleanerType(CleanerEnum.CLEANER_TYPE_SWEEP.getValue());
            List<Cleaner> cleanerList = cleanerService.selectCleanerList(cleaner);
            if (CollectionUtil.isEmpty(cleanerList)) {
                return orderAndHouseSweep(houseSweep, order, house, cleaningProcessList);
            }
            List<Cleaner> newCleanerList = new ArrayList<>();
            for (Cleaner cleaner1 : cleanerList) {
                if (cleaner1.getCleanerScope().equals(CleanerEnum.CLEANER_SCOPE_ALL.getValue())) {
                    newCleanerList.add(cleaner1);
                } else {
                    if (cleaner1.getEstateIds().contains(house.getEstateId())) {
                        newCleanerList.add(cleaner1);
                    }
                }
            }
            if (CollectionUtil.isEmpty(newCleanerList)) {
                return orderAndHouseSweep(houseSweep, order, house, cleaningProcessList);
            }
            List<Long> cleanerIds = newCleanerList.stream().map(Cleaner::getCleanerId).collect(Collectors.toList());
            Long[] cleanerIdsArray = cleanerIds.toArray(new Long[cleanerIds.size()]);
            cleaner.setCleanerIds(cleanerIdsArray);
            cleaner.setCreateTime(new Date());


            List<Cleaner> cleaners = cleanerService.selectCleanerByCleanerList(cleaner);
            List<Long> clenanerIdList = cleaners.stream().map(Cleaner::getCleanerId).collect(Collectors.toList());
            if (clenanerIdList.size() == 0) {
                Random random = new Random();
                int nexted = random.nextInt(newCleanerList.size());
                Long cleanerId = cleanerIds.get(nexted);
                houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_EXCHANGE.getValue());
                return sendHouseSweep(houseSweep, order, house, cleaningProcessList, cleanerId);

            } else {
                if (cleanerIds.size() > clenanerIdList.size()) {
                    cleanerIds.removeAll(clenanerIdList);
                    Random random = new Random();
                    int nexted = random.nextInt(cleanerIds.size());
                    Long cleanerId = cleanerIds.get(nexted);
                    houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_EXCHANGE.getValue());
                    return sendHouseSweep(houseSweep, order, house, cleaningProcessList, cleanerId);
                } else {
                    Random random = new Random();
                    int nexted = random.nextInt(cleanerIds.size());
                    Long cleanerId = cleanerIds.get(nexted);
                    houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_EXCHANGE.getValue());
                    return sendHouseSweep(houseSweep, order, house, cleaningProcessList, cleanerId);
                }

            }
        }


        return orderAndHouseSweep(houseSweep, order, house, cleaningProcessList);
    }

    private int orderAndHouseSweep(HouseSweep houseSweep, Order order, House house, List<CleaningProcess> cleaningProcessList) {
        houseSweep.setSweepState(HouseSweepEnum.SWEEP_STATE_NO.getValue());
        houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_EXCHANGE.getValue());
        houseSweep.setFullAddress(house.getEstateName() + house.getBuildingName() + "栋" + house.getUnitName() + "单元" + house.getRoomNumber());
        houseSweep.setOrderId(order.getOrderId());
        houseSweep.setHouseId(house.getHouseId());
        String houseSweepId = "W" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 100000);
        houseSweep.setHouseSweepId(houseSweepId);
        houseSweep.setCreateTime(DateUtils.getNowDate());
        houseSweepMapper.insertHouseSweep(houseSweep);

        List<SweepProcessRecord> sweepProcessRecordList = new ArrayList<>();
        for (CleaningProcess process : cleaningProcessList) {
            SweepProcessRecord sweepProcessRecord = new SweepProcessRecord();
            sweepProcessRecord.setHouseSweepId(houseSweep.getHouseSweepId());
            sweepProcessRecord.setTenantId(houseSweep.getTenantId());
            sweepProcessRecord.setCleaningProcessName(process.getCleaningProcessName());
            sweepProcessRecord.setSort(process.getSort());
            sweepProcessRecord.setCreateTime(new Date());
            sweepProcessRecordList.add(sweepProcessRecord);
        }

        sweepProcessRecordService.insertSweepProcessRecordList(sweepProcessRecordList);
        return 1;
    }

    @Override
    public List<HouseSweep> selectHouseSweepByCleanerIds(Long[] cleanerIds) {
        return houseSweepMapper.selectHouseSweepByCleanerIds(cleanerIds);
    }

    @Override
    public Integer selectHouseSweepNumber(String tenantId, Long userId) {
        return houseSweepMapper.selectHouseSweepNumber(tenantId, userId);
    }

    @Override
    public HouseSweep selectHouseSweepByHouseId(String houseId) {
        return houseSweepMapper.selectHouseSweepByHouseId(houseId);
    }

    @Override
    public int insertHouseSweep(HouseSweep houseSweep) {
        return houseSweepMapper.insertHouseSweep(houseSweep);
    }

    /**
     * 指定打扫人员
     *
     * @param houseSweep 打扫记录
     * @return 结果
     */
    @Override
    public int updateHouseSweep(HouseSweep houseSweep) {
        HouseSweep byHouseSweepId = houseSweepMapper.selectHouseSweepByHouseSweepId(houseSweep.getHouseSweepId());
        if (!TenantContext.getTenantId().equals(byHouseSweepId.getTenantId())) {
            return 0;
        }
        if (!byHouseSweepId.getSweepState().equals(HouseInspectionEnum.INSPECTION_STATE_NO.getValue())) {
            throw new ServiceException("数据状态异常，请刷新后再试");
        }
        if (houseSweep.getCleanerId() == null || houseSweep.getCleanerId().equals("")) {
            throw new ServiceException("请选择打扫人");
        }
        Cleaner cleaner = cleanerService.selectCleanerByCleanerId(houseSweep.getCleanerId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("打扫员不存在");
        }
        houseSweep.setSweepState(HouseInspectionEnum.INSPECTION_STATE_YES.getValue());
        houseSweep.setHomeSweepName(cleaner.getCleanerName());
        houseSweep.setUpdateTime(DateUtils.getNowDate());
        houseSweep.setCleanerUserId(cleaner.getCleanerUserId());
        houseSweep.setVersion(byHouseSweepId.getVersion());
        int i = houseSweepMapper.updateHouseSweep(houseSweep);
        if (i == 0) {
            return 0;
        }


        // 短信通知
        BizUserBusiness userBusiness = bizUserBusinessService.selectBizUserBusinessByUserId(cleaner.getCleanerUserId(), TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(userBusiness)) {
            throw new ServiceException("企业用户不存在");
        }
        Boolean smsWithData = smsUtils.sendTemplateSmsWithData(userBusiness.getPhonenumber(), smsConfig.getSweepTemplateCode(), new String[]{DateUtil.formatDateTime(byHouseSweepId.getScheduledTime()), byHouseSweepId.getFullAddress()});
        if (smsWithData) {
            return 1;
        }
        return 0;
    }

    /**
     * 更换打扫人员
     */
    @Override
    @Transactional
    public int changeHouseSweep(HouseSweep houseSweep) {
        HouseSweep byHouseSweepId = houseSweepMapper.selectHouseSweepByHouseSweepId(houseSweep.getHouseSweepId());
        if (!TenantContext.getTenantId().equals(byHouseSweepId.getTenantId())) {
            return 0;
        }
        if (!byHouseSweepId.getSweepState().equals(HouseInspectionEnum.INSPECTION_STATE_YES.getValue())) {
            throw new ServiceException("数据状态异常，请刷新后再试");
        }
        if (houseSweep.getCleanerId() == null || houseSweep.getCleanerId().equals("")) {
            throw new ServiceException("请选择打扫人");
        }
        Cleaner cleaner = cleanerService.selectCleanerByCleanerId(byHouseSweepId.getCleanerId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("打扫员不存在");
        }
        Cleaner newCleaner = cleanerService.selectCleanerByCleanerId(houseSweep.getCleanerId());
        if (ObjectUtil.isEmpty(newCleaner)) {
            throw new ServiceException("打扫员不存在");
        }
        String homeSweepName = byHouseSweepId.getHomeSweepName();

        houseSweep.setSweepState(HouseInspectionEnum.INSPECTION_STATE_YES.getValue());
        houseSweep.setHomeSweepName(newCleaner.getCleanerName());
        houseSweep.setUpdateTime(DateUtils.getNowDate());
        houseSweep.setCleanerUserId(newCleaner.getCleanerUserId());
        houseSweep.setVersion(byHouseSweepId.getVersion());
        int i = houseSweepMapper.updateHouseSweep(houseSweep);
        if (i == 0) {
            return 0;
        }

        // 短信通知
        BizUserBusiness userBusiness = bizUserBusinessService.selectBizUserBusinessByUserId(cleaner.getCleanerUserId(), TenantContext.getTenantId());
        BizUserBusiness newUserBusiness = bizUserBusinessService.selectBizUserBusinessByUserId(newCleaner.getCleanerUserId(), TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(userBusiness) || ObjectUtil.isEmpty(newUserBusiness)) {
            throw new ServiceException("企业用户不存在");
        }
        Boolean smsWithData = smsUtils.sendTemplateSmsWithData(userBusiness.getPhonenumber(), smsConfig.getExchangeSweepTemplateCode(), new String[]{homeSweepName, DateUtil.formatDateTime(byHouseSweepId.getScheduledTime()), byHouseSweepId.getFullAddress()});
        Boolean smsData = smsUtils.sendTemplateSmsWithData(newUserBusiness.getPhonenumber(), smsConfig.getSweepTemplateCode(), new String[]{DateUtil.formatDateTime(byHouseSweepId.getScheduledTime()), byHouseSweepId.getFullAddress()});
        if (smsWithData && smsData) {
            return 1;
        }
        return 0;
    }


    /**
     * 新增日常打扫记录
     */
    @Override
    @Transactional
    public int routineSweep(HouseSweep houseSweep) {
        if (houseSweep.getScheduledTime() == null || houseSweep.getScheduledTime().equals("")) {
            throw new ServiceException("请选择预计开始时间");
        }
        if (StrUtil.isEmpty(houseSweep.getHouseId())) {
            throw new ServiceException("请选择房间");
        }

        House house = houseService.selectHouseById(houseSweep.getHouseId());
        if (house == null) {
            throw new ServiceException("房源不存在");
        }
        Tenant tenant = departmentService.selectDepByDepId(TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            throw new ServiceException("租户不存在");
        }

        CleaningProcess cleaningProcess = new CleaningProcess();
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        cleaningProcess.setHouseId(house.getHouseId());
        List<CleaningProcess> cleaningProcessList = cleaningProcessService.selectCleaningProcessListAndParentId(cleaningProcess);
        if (CollectionUtil.isEmpty(cleaningProcessList)) {
            throw new ServiceException("房间未设置保洁流程");
        }


        // 自动派单
        if (TenantConstants.ORDER_ALLOCATION_STATE_YES.equals(tenant.getOrderAllocationState())) {
            Cleaner cleaner = new Cleaner();
            cleaner.setTenantId(TenantContext.getTenantId());
            cleaner.setCleanerType(CleanerEnum.CLEANER_TYPE_SWEEP.getValue());
            List<Cleaner> cleanerList = cleanerService.selectCleanerList(cleaner);
            if (CollectionUtil.isEmpty(cleanerList)) {
                return HouseSweep(houseSweep, house, cleaningProcessList);
            }
            List<Cleaner> newCleanerList = new ArrayList<>();
            for (Cleaner cleaner1 : cleanerList) {
                if (cleaner1.getCleanerScope().equals(CleanerEnum.CLEANER_SCOPE_ALL.getValue())) {
                    newCleanerList.add(cleaner1);
                } else {
                    if (cleaner1.getEstateIds().contains(house.getEstateId())) {
                        newCleanerList.add(cleaner1);
                    }
                }
            }
            if (CollectionUtil.isEmpty(newCleanerList)) {
                return HouseSweep(houseSweep, house, cleaningProcessList);
            }
            List<Long> cleanerIds = newCleanerList.stream().map(Cleaner::getCleanerId).collect(Collectors.toList());
            Long[] cleanerIdsArray = cleanerIds.toArray(new Long[cleanerIds.size()]);
            cleaner.setCleanerIds(cleanerIdsArray);
            cleaner.setCreateTime(new Date());


            List<Cleaner> cleaners = cleanerService.selectCleanerByCleanerList(cleaner);
            List<Long> clenanerIdList = cleaners.stream().map(Cleaner::getCleanerId).collect(Collectors.toList());
            if (clenanerIdList.size() == 0) {
                Random random = new Random();
                int nexted = random.nextInt(newCleanerList.size());
                Long cleanerId = cleanerIds.get(nexted);
                houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_ORDINARILY.getValue());
                return sendHouseSweep(houseSweep, null, house, cleaningProcessList, cleanerId);

            } else {
                if (cleanerIds.size() > clenanerIdList.size()) {
                    cleanerIds.removeAll(clenanerIdList);
                    Random random = new Random();
                    int nexted = random.nextInt(cleanerIds.size());
                    Long cleanerId = cleanerIds.get(nexted);
                    houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_ORDINARILY.getValue());
                    return sendHouseSweep(houseSweep, null, house, cleaningProcessList, cleanerId);
                } else {
                    Random random = new Random();
                    int nexted = random.nextInt(cleanerIds.size());
                    Long cleanerId = cleanerIds.get(nexted);
                    houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_ORDINARILY.getValue());
                    return sendHouseSweep(houseSweep, null, house, cleaningProcessList, cleanerId);
                }

            }
        }

        return HouseSweep(houseSweep, house, cleaningProcessList);
    }

    private int HouseSweep(HouseSweep houseSweep, House house, List<CleaningProcess> cleaningProcessList) {
        houseSweep.setSweepState(HouseSweepEnum.SWEEP_STATE_NO.getValue());
        houseSweep.setSweepType(HouseSweepEnum.SWEEP_TYPE_ORDINARILY.getValue());
        houseSweep.setFullAddress(house.getEstateName() + house.getBuildingName() + "栋" + house.getUnitName() + "单元" + house.getRoomNumber());
        houseSweep.setHouseId(house.getHouseId());
        String houseSweepId = "W" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 100000);
        houseSweep.setHouseSweepId(houseSweepId);
        houseSweep.setCreateTime(DateUtils.getNowDate());
        houseSweepMapper.insertHouseSweep(houseSweep);

        List<SweepProcessRecord> sweepProcessRecordList = new ArrayList<>();
        for (CleaningProcess process : cleaningProcessList) {
            SweepProcessRecord sweepProcessRecord = new SweepProcessRecord();
            sweepProcessRecord.setHouseSweepId(houseSweep.getHouseSweepId());
            sweepProcessRecord.setTenantId(houseSweep.getTenantId());
            sweepProcessRecord.setCleaningProcessName(process.getCleaningProcessName());
            sweepProcessRecord.setSort(process.getSort());
            sweepProcessRecord.setCreateTime(new Date());
            sweepProcessRecordList.add(sweepProcessRecord);
        }

        sweepProcessRecordService.insertSweepProcessRecordList(sweepProcessRecordList);
        return 1;
    }

    @Override
    @Transactional
    public int editHouseSweepList(HouseSweep houseSweep) {
        HouseSweep sweep = houseSweepMapper.selectHouseSweepByHouseSweepId(houseSweep.getHouseSweepId());
        if (ObjectUtil.isEmpty(sweep)) {
            throw new ServiceException("数据已被删除，请刷新后再试");
        }
        if (sweep.getSweepState().equals(HouseInspectionEnum.INSPECTION_STATE_NO.getValue())) {
            throw new ServiceException("数据状态异常，请刷新后再试");
        }
        if (!sweep.getCleanerUserId().equals(houseSweep.getCleanerUserId())) {
            throw new ServiceException("打扫人数据异常，请刷新后再试");
        }

        List<HousePicture> housePictureList = new ArrayList<>();
        List<SweepProcessRecord> sweepProcessRecordList = houseSweep.getSweepProcessRecordList();
        // 生成分组编号
        String packetIdentification = RandomUtil.randomStringWithoutStr(10, "0123456789");
        for (SweepProcessRecord sweepProcessRecord : sweepProcessRecordList) {
            if (StrUtil.isEmpty(sweepProcessRecord.getFacilitiesImg())) {
                throw new ServiceException(sweepProcessRecord.getCleaningProcessName() + "请拍摄打扫后图片");
            }
            String facilitiesImg = sweepProcessRecord.getFacilitiesImg();
            if (StrUtil.isNotEmpty(facilitiesImg)) {
                String[] split = facilitiesImg.split(",");
                if (split.length > 9) {
                    throw new ServiceException("最多上次9张打扫后图片");
                }
            }
            sweepProcessRecord.setUpdateTime(new Date());


            // 封装房屋相册记录
            HousePicture housePicture = new HousePicture(sweep.getHouseId(), sweep.getFullAddress(), HouseConstants.HOUSE_PICTURE_SWEEP, houseSweep.getUpdateBy(),
                    houseSweep.getOperatorId(), facilitiesImg.split(",").length, facilitiesImg, sweep.getTenantId(),
                    sweepProcessRecord.getCleaningProcessName(), null, sweep.getHouseSweepId(), packetIdentification);
            housePicture.setCreateTime(new Date());
            housePictureList.add(housePicture);

        }
        houseSweep.setSweepState(HouseSweepEnum.SWEEP_STATE_FULFILL.getValue());
        houseSweep.setFinishTime(new Date());
        houseSweep.setVersion(sweep.getVersion());
        int i = houseSweepMapper.updateHouseSweep(houseSweep);
        if (i == 0) {
            return 0;
        }


        // 新增房屋相册信息
        int j = housePictureService.insertBatchHousePicture(housePictureList);
        if (j == 0) {
            return 0;
        }
        return sweepProcessRecordService.updateSweepProcessRecordList(sweepProcessRecordList);
    }

    @Override
    public HouseSweep selectHouseSweepByOrderId(String orderId) {
        return houseSweepMapper.selectHouseSweepByOrderId(orderId);
    }


    /**
     * 批量删除打扫记录
     *
     * @param houseSweepIds 需要删除的打扫记录主键
     * @return 结果
     */
    @Override
    public int deleteHouseSweepByHouseSweepIds(String[] houseSweepIds) {
        HouseSweep houseSweep = new HouseSweep();
        houseSweep.setHouseSweepIds(houseSweepIds);
        houseSweep.setTenantId(TenantContext.getTenantId());
        List<HouseSweep> houseSweepList = houseSweepMapper.selectHouseSweepByHouseSweepIds(houseSweep);
        if (houseSweepList.size() < houseSweepIds.length) {
            return 0;
        }
        houseSweepMapper.updateHouseSweepByHouseSweepIds(houseSweep);
        SweepProcessRecord sweepProcessRecord = new SweepProcessRecord();
        sweepProcessRecord.setHouseSweepIds(houseSweepIds);
        sweepProcessRecord.setTenantId(TenantContext.getTenantId());
        sweepProcessRecordService.updateSweepProcessRecordByHouseSweepIds(sweepProcessRecord);
        return 1;
    }

    /**
     * 删除打扫记录信息
     *
     * @param houseSweepId 打扫记录主键
     * @return 结果
     */
    @Override
    public int deleteHouseSweepByHouseSweepId(String houseSweepId) {
        return houseSweepMapper.deleteHouseSweepByHouseSweepId(houseSweepId);
    }

    private int sendHouseSweep(HouseSweep houseSweep, Order order, House house, List<CleaningProcess> cleaningProcessList, Long cleanerId) {
        Cleaner byCleanerId = cleanerService.selectCleanerByCleanerId(cleanerId);
        if (ObjectUtil.isEmpty(byCleanerId)) {
            throw new ServiceException("保洁人员不存在");
        }

        houseSweep.setOperatorId(houseSweep.getOperatorId());
        houseSweep.setTenantId(houseSweep.getTenantId());
        houseSweep.setScheduledTime(houseSweep.getScheduledTime());
        houseSweep.setSweepState(HouseSweepEnum.SWEEP_STATE_YES.getValue());
        houseSweep.setFullAddress(house.getEstateName() + house.getBuildingName() + "栋" + house.getUnitName() + "单元" + house.getRoomNumber());
        if (ObjectUtil.isNotEmpty(order)) {
            houseSweep.setOrderId(order.getOrderId());
        }
        houseSweep.setHouseId(house.getHouseId());
        String houseSweepId = "W" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 100000);
        houseSweep.setHouseSweepId(houseSweepId);
        houseSweep.setCreateTime(DateUtils.getNowDate());
        houseSweep.setHomeSweepName(byCleanerId.getCleanerName());
        houseSweep.setCleanerUserId(byCleanerId.getCleanerUserId());
        houseSweep.setCleanerId(byCleanerId.getCleanerId());
        houseSweepMapper.insertHouseSweep(houseSweep);

        List<SweepProcessRecord> sweepProcessRecordList = new ArrayList<>();
        for (CleaningProcess process : cleaningProcessList) {

            if (process.getParentId() == null) {
                SweepProcessRecord sweepProcessRecord = new SweepProcessRecord();
                sweepProcessRecord.setHouseSweepId(houseSweep.getHouseSweepId());
                sweepProcessRecord.setTenantId(houseSweep.getTenantId());
                sweepProcessRecord.setCleaningProcessName(process.getCleaningProcessName());
                sweepProcessRecord.setSort(process.getSort());
                sweepProcessRecord.setCreateTime(new Date());
                sweepProcessRecordList.add(sweepProcessRecord);
            }
        }
        sweepProcessRecordService.insertSweepProcessRecordList(sweepProcessRecordList);


        // 短信通知
        BizUserBusiness userBusiness = bizUserBusinessService.selectBizUserBusinessByUserId(byCleanerId.getCleanerUserId(), TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(userBusiness)) {
            throw new ServiceException("企业用户不存在");
        }
        Boolean smsSweepData = smsUtils.sendTemplateSmsWithData(userBusiness.getPhonenumber(), smsConfig.getSweepTemplateCode(), new String[]{DateUtil.formatDateTime(houseSweep.getScheduledTime()), houseSweep.getFullAddress()});
        if (smsSweepData) {
            return 1;
        }
        return 0;

    }
}
